home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
53142
/
53142.xpi
/
content
/
soaccount.js
< prev
next >
Wrap
Text File
|
2009-12-06
|
9KB
|
290 lines
/*##########################################################################
Copyright 2009 Tim Reid
This file is part of the Stack Overflow Reputation Display extension
for Mozilla Firefox.
Stack Overflow Reputation Display is free software: you can
redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
Stack Overflow Reputation Display is distributed in the hope that it
will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public
License along with Stack Overflow Reputation Display. If not,
see <http://www.gnu.org/licenses/>.
##########################################################################*/
var SOAccount = function (init) {
if (typeof init == "object") {
for (var k in init)
this[k] = init[k];
} else if (typeof init == "string") {
this.config = init;
}
this.observers = [];
return this;
};
SOAccount.prototype = {
_site: null,
_user: null,
sounds: null,
dosounds: false,
_updateinterval: 900,
clickaction: "main",
debug: false,
intervaltimerid: null,
clone: function () {
var c = new SOAccount(this);
delete c.intervaltimerid;
return c;
},
wantspageloads: function () {
return !this.user;
},
interfaces: {
sound: Components.classes["@mozilla.org/sound;1"]
.createInstance(Components.interfaces.nsISound),
console: Components.classes["@mozilla.org/consoleservice;1"]
.getService(Components.interfaces.nsIConsoleService),
ios: Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService),
},
toString: function () {
return "[object SOAccount: " + this.config + "]";
},
get config () {
var config = this.site.sitetag;
config += ":";
if (this.user && this.user != SOAccount.prototype.user)
config += this.user;
config += ":";
if (this.updateinterval && this.updateinterval != SOAccount.prototype.updateinterval)
config += this.updateinterval;
config += ":";
if (this.clickaction && this.clickaction != SOAccount.prototype.clickaction)
config += this.clickaction;
config += ":";
if (this.dosounds && this.dosounds != SOAccount.prototype.dosounds)
config += this.dosounds;
config = config.replace(/:+$/, "");
return config;
},
set config (config) {
var parts = config.split(/:/);
var labels = [ "site", "user", "updateinterval", "clickaction", "dosounds", ]
var fields = {};
if (parts[0]) fields.site = new SOSite(parts[0]);
if (parts[1]) fields.user = parts[1];
if (parts[2]) fields.updateinterval = parseInt(parts[2]);
if (parts[3]) fields.clickaction = parts[3];
if (parts[4]) fields.dosounds = (parts[4] == "true");
for (var i=0; i<labels.length; i++)
if (labels[i] in fields) {
if (this[labels[i]] != fields[labels[i]])
this[labels[i]] = fields[labels[i]];
} else {
delete this[labels[i]];
}
},
get datauri () {
return this.site.uri("data", this.user);
},
get site () {
return this._site;
},
set site (s) {
this._site = s;
this.user = null;
},
get user () {
return this._user;
},
set user (u) {
this._user = u;
delete this.info;
},
get name () {
if (this.info)
return this.info.name;
return null;
},
get updateinterval () {
return this._updateinterval;
},
set updateinterval (i) {
this._updateinterval = i;
if (this.intervaltimerid)
this.launchtimer();
},
cleartimer: function () {
if (this.debug)
this.interfaces.console.logStringMessage("SOAccount clearing update inverval timer");
if (this.intervaltimerid) {
window.clearInterval(this.intervaltimerid);
delete this.intervaltimerid;
}
},
launchtimer: function () {
if (this.user) {
var me = this;
this.cleartimer();
if (this.debug)
this.interfaces.console.logStringMessage("SOAccount setting update inverval to " + this.updateinterval + " seconds");
this.sendrequest();
this.intervaltimerid = window.setInterval(function () {
me.sendrequest();
},
this.updateinterval * 1000);
}
},
sendrequest: function () {
var datauri = this.datauri;
if (datauri) {
var me = this;
var req = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
if (this.debug)
this.interfaces.console.logStringMessage("SOAccount sending request (" + datauri + ")...");
req.onload = function (aEvt) { me.handleresponse(req.responseText); };
req.open("GET", datauri, true);
req.send(null);
}
},
addObserver: function (observer) {
this.observers.push(observer);
},
removeObserver: function (observer) {
var newobservers = [];
for (var i=0; i<this.observers.length; i++)
if (this.observers[i] !== observer)
newobservers.push(this.observers[i]);
this.observers = newobservers;
},
handleresponse: function (responsetext) {
if (this.debug)
this.interfaces.console.logStringMessage("SOAccount handling response...");
var newinfo = this.site.extractinfo(responsetext);
if (!newinfo)
return;
var currentinfo = this.info;
this.info = newinfo;
var change;
if (currentinfo) {
if (this.debug)
this.interfaces.console.logStringMessage("SOAccount comparing new details with current...");
if (newinfo.reputation &&
"reputation" in currentinfo) {
if (newinfo.reputation > currentinfo.reputation) {
change = change || "up";
} else if (newinfo.reputation < currentinfo.reputation) {
change = change || "down";
}
}
var badges = this.site.badges;
for (var i=0; i<badges.length; i++) {
var badge = badges[i];
if (newinfo[badge] &&
currentinfo[badge]) {
if (newinfo[badge] > currentinfo[badge]) {
change = change || "up";
} else if (newinfo[badge] < currentinfo[badge]) {
change = change || "down";
}
}
}
if (change)
if (change == "up")
this.dosound("up");
else if (change == "down")
this.dosound("down");
}
for (var i=0; i<this.observers.length; i++)
try {
this.observers[i].observe(this, "soaccount:scorechanged", change);
} finally {
}
},
dosound: function (tag) {
if (this.debug)
this.interfaces.console.logStringMessage("SOAccount dosound() called for: " + tag);
if (this.dosounds && this.sounduris) {
if (tag in this.sounduris) {
if (this.debug)
this.interfaces.console.logStringMessage("SOAccount found sound: " + this.sounduris[tag]);
this.interfaces.sound.play(this.interfaces
.ios
.newURI(this.sounduris[tag], null, null)
.QueryInterface(Components.interfaces.nsIURL));
} else {
var scriptError = Components.classes["@mozilla.org/scripterror;1"]
.createInstance(Components.interfaces.nsIScriptError);
scriptError.init("SOAccount dosound() called with unknown sound \"" + tag + "\"",
"chrome://sorepdisplay/content/soaccount.js",
null,
null,
null,
nsIScriptError.errorFlag,
null);
this.interfaces.console.logMessage(scriptError);
}
}
},
pageload: function (doc) {
var user = this.site.finduser(doc);
if (user && user != this.user) {
this.user = user;
this.launchtimer();
return true;
}
return false;
},
};